package ArrayList;


import java.util.Arrays;

public class SequenceList<T> {
    static int DEFAULT_CAPACITY = 10;
    private int size;//元素的个数
    private int capacity;//保存数组的长度
    private Object[] elementData;

    //以默认容量创建空顺序线性表
    public SequenceList() {
        capacity = DEFAULT_CAPACITY;
        elementData = new Object[capacity];
    }

    public SequenceList(int initCapacity) {
        capacity = initCapacity;
        elementData = new Object[initCapacity];
    }

    public static void main(String[] args) {
        SequenceList<String> ss = new SequenceList<String>();
        ss.add("123");
        ss.add("234");
        ss.add("345");
        for (int i = 0; i < 6; i++) {
            ss.add(i + "001");
        }
        ss.add("111", 1);
        ss.add("12345", 2);
        System.out.println("数组内容：" + ss);

        System.out.println("线性表大小：" + ss.size());

        System.out.println("----------by index get element-------------");
        System.out.println(ss.get(3));
        System.out.println(ss.get("345"));
        System.out.println("---------------------------------");
        //删除线性表索引指定的值
        System.out.println(ss.remove(9));
        //System.out.println(ss.remove(1));
        System.out.println("----------delete List index------");
        System.out.println(ss);
        System.out.println("线性表大小：" + ss.size());

        ss.clear();
        System.out.println("----------clear List-------------6226 0978 0264 9286");
        System.out.println(ss);

        System.out.println(ss.capacity);
    }

    //删除元素

    //添加元素
    public void add(T element, int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("线性表索引越界");
        }
        ensureCapacity(size + 1);

        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = element;
        size++;
    }

    @SuppressWarnings("unchecked")
    public T remove(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("线性表索引越界");
        }

        T oldValue = (T) elementData[index];

        int newSize = size - 1 - index;
        if (newSize > 0) {
            System.arraycopy(elementData, index + 1, elementData, index, newSize);
        }

        elementData[--size] = null;

        return oldValue;
    }

    public void add(T element) {
        add(element, size);
    }

    private void ensureCapacity(int newCapacity) {
        // TODO Auto-generated method stub
        if (newCapacity > capacity) {
            while (capacity < newCapacity) {
                capacity <<= 1;
            }
            elementData = Arrays.copyOf(elementData, capacity);
            System.out.println("----****-----" + elementData.length);
        }
    }

    //获取元素的大小
    public int size() {
        return size;
    }

    //清空线性表
    public void clear() {
        Arrays.fill(elementData, null);
        size = 0;
    }

    //查找线性表的索引或者值
    @SuppressWarnings("unchecked")
    public T get(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("线性数组越界");
        }

        return (T) elementData[index];
    }

    public int get(T element) {
        for (int i = 0; i < elementData.length; i++) {
            if (elementData[i].equals(element)) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public String toString() {

        if (size == 0) {
            return "[]";
        }

        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(elementData[i] + ",");
        }
        sb.append("]");
        sb.delete(sb.length() - 2, sb.length() - 1);
        return sb.toString();
    }

}
